//
//  ViewController.m
//  11206
//
//  Created by student on 16/3/3.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
static NSString *identifer=@"CELL";
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating>
@property(nonatomic,strong) NSDictionary *fc;
@property(nonatomic,strong) NSArray *keys;
@property(nonatomic,strong) UISearchController *searchcontrol;
@property(nonatomic,strong) NSMutableArray *filternames;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *path=[[NSBundle mainBundle]pathForResource:@"足球队dictionary" ofType:@"plist"];
    self.fc=[NSDictionary dictionaryWithContentsOfFile:path];
    self.keys=[[self.fc allKeys] sortedArrayUsingSelector:@selector(compare:)];
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:identifer];
    
    self.tableview.tag=1;
    
    UISearchBar *searchbar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 375, 44)];
    self.tableview.tableHeaderView=searchbar;
    
    
    self.searchcontrol=[[UISearchController alloc]initWithSearchResultsController:nil];
    self.searchcontrol.searchResultsUpdater=self;
    self.tableview.tableHeaderView=self.searchcontrol.searchBar;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if(self.searchcontrol.active)
    {
        return 1;
    }
    else
    {
        return self.keys.count;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(self.searchcontrol.active)
    {
        return self.filternames.count;
    }
    else
    {

        NSString *key = self.keys[section];
        NSArray *arr = self.fc[key];
        return arr.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if(self.searchcontrol.active)
    {
        cell.textLabel.text=self.filternames[indexPath.row];
        return cell;
    }
    else
    {
    NSString *key = self.keys[indexPath.section];
    NSArray *arr = self.fc[key];
    cell.textLabel.text = arr[indexPath.row];
    return cell;
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(self.searchcontrol.active)
    {
        return nil;
    }
    else
    {
    NSString *key = self.keys[section];
    return key;
    }
}





- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if(self.searchcontrol.active)
    {
        return nil;
    }
    else
    {
    return self.keys;
    }
}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *searchString=searchController.searchBar.text;
    if(searchString.length>0)
    {
        self.filternames=[NSMutableArray array];
        
        NSPredicate *pre=[NSPredicate predicateWithBlock:^BOOL(NSString *name,NSDictionary<NSString *,id> * _Nullable bindings){
            NSRange range=[name rangeOfString:searchString options:NSCaseInsensitiveSearch];
            return range.location !=NSNotFound;
        }];
        
        
       for(NSString *key in self.keys)
       {
           NSArray *names=self.fc[key];
           NSArray *tmp=[names filteredArrayUsingPredicate:pre];
           [self.filternames addObjectsFromArray:tmp];
       }
    }
    [self.tableview reloadData];
}

@end
